package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by LZQ on 2017/12/13.
 */

public class RecordUsername_RVAdapter extends RecyclerView.Adapter<RecordUsername_RVAdapter.mViewHolder> {

    private List<String> mData;
    private LayoutInflater layoutInflater;

    private OnClickCallBack mCallBack;

    public RecordUsername_RVAdapter(Context context, List<String> mData) {
        this.mData = mData;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public mViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = layoutInflater.inflate(R.layout.item_login_username, viewGroup, false);
        return new mViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final mViewHolder mViewHolder, int i) {
        mViewHolder.tvLoginUsername.setText(mData.get(i));
        mViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mCallBack != null){
                    mCallBack.onItemClick(view, mViewHolder.getAdapterPosition(), mData);
                }
            }
        });
        mViewHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mCallBack != null){
                    mCallBack.onDeleteClick(view, mViewHolder.getAdapterPosition(), mData);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public void removeItem(int position){
        mData.remove(position);
        notifyItemRemoved(position);
    }

    class mViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_login_username)
        TextView tvLoginUsername;
        @BindView(R.id.iv_delete)
        ImageView ivDelete;
        public mViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnClickCallBack{
        void onItemClick(View view, int position, List<String> recordList);
        void onDeleteClick(View view, int position, List<String> recordList);
    }

    public void setOnClickCallBack(OnClickCallBack clickCallBack){
        this.mCallBack = clickCallBack;
    }
}
